﻿
using System;
using System.Collections.Generic;
using System.Text;
using DCI6.Client.API;
using DCI6.Client;
using DCI6;
using DCI6.Client.Payloads1314.c2r;
using DCI6.Client.Payloads1314.aco;
using DCI6.Client.Payloads1314.cpofx;
using DCI6.Client.Payloads1314.enemysit;
using DCI6.Client.Payloads1314.forcetckr;
using DCI6.Client.Payloads1314.georef;
using DCI6.Client.Payloads1314.graphics;
using DCI6.Client.Payloads1314.indwarn;
using DCI6.Client.Payloads1314.mtirpt;
using DCI6.Client.Payloads1314.obspos;
using DCI6.Client.Payloads1314.orgstat;
using DCI6.Client.Payloads1314.posrpt;
using DCI6.Client.Payloads1314.airdefense;
using DCI6.Client.Payloads1314.rangefan;
using DCI6.Client.Payloads1314.sigact;
using DCI6.Client.Payloads1314.symbol;
using DCI6.Client.Payloads1314.target1314;
using DCI6.Client.Payloads1314.taskorg;
using DCI6.Client.Payloads1314.groundtrack;
using DCI6.Client.Payloads1314.uav;
using DCI6.Client.Payloads1314.weather2;

namespace DCIAPIRMSampleCode
{
  
  class CallbackListener : DCIClient.Adaptor
  {

    #region DCIMessageListener Members

    public override void DDSConnectionStatusChanged(bool connectionFlag, string ddsID)
    {
      Console.WriteLine("DDSConnectionStatusChanged {connectionFlag=" + connectionFlag + "}");
      Console.WriteLine();
    }

    public override void dciResubsciptionNotificationReceived(string origID, string newID, string ddsID)
    {
      Console.WriteLine("dciResubscriptionNotificationReceived {sID=" + newID + "}");
      Console.WriteLine();
    }

    public override void dciShutdownNotificationReceived(string timeStamp)
    {
      Console.WriteLine("dciShutdownNotificationReceived timeStamp:" + timeStamp);
      Console.WriteLine();
    }

    public override void dciStartupNotificationReceived(string timeStamp)
    {
      Console.WriteLine("dciStartupNotificationReceived timeStamp:" + timeStamp);
      Console.WriteLine();
    }

    public override void retractItemNotificationReceived(string advertisementId, string itemId, string ddsID)
    {
      Console.WriteLine("retractItemNotificationReceived {advertisementId=" + advertisementId + ", itemID=" + itemId + "}");
      Console.WriteLine();
    }

    public override void validQueryPublicationReceived(string queryID, bool complete, List<Item> items, string ddsID)
    {
      Console.WriteLine("validQueryPublicationReceived");
      Console.WriteLine();
    }

    public override void invalidQueryPublicationReceived(string queryID, bool complete, List<Item> items, string ddsID)
    {
      Console.WriteLine("invalidQueryPublicationReceived");
      Console.WriteLine();
    }

    public override void invalidPublicationReceived(List<Item> items, string ddsID)
    {
      foreach (Item item in items)
      {
        Console.WriteLine("invalidPublicationReceived {Item=" + item.ItemID + "}");
        Console.WriteLine();
      }
    }

    public override void validPublicationReceived(List<Item> items, string ddsID)
    {
        foreach (Item item in items)
        {
            Console.WriteLine("validPublicationReceived {Item=" + item.ItemID + "}");
            Console.WriteLine("ItemData XML: " + item.DDSItem.ItemData.AnyElement.ToXml());
            String rootTag = item.RootTag;
            //Get XML of 1st payload
            String xml = item.DDSItem.ItemData.AnyElement[0].ToXml();
            try
            {
                if (rootTag.Equals("airspace_control_order"))
                {
                    Console.WriteLine("Received airspace_control_order");
                    Airspace_control_order obj = new Airspace_control_order();
                    obj.FromXml(xml);
                    Console.WriteLine("Object XML: " + obj.ToXml());
                }
                else if (rootTag.Equals("C2rChanges"))
                {
                    Console.WriteLine("Received C2rChanges");
                    C2rChanges obj = new C2rChanges();
                    obj.FromXml(xml);
                    Console.WriteLine("Object XML: " + obj.ToXml());
                }
                else if (rootTag.Equals("CPOF_MSG"))
                {
                    Console.WriteLine("Received CPOF_MSG");
                    CPOF_MSG obj = new CPOF_MSG();
                    obj.FromXml(xml);
                    Console.WriteLine("Object XML: " + obj.ToXml());
                }
                else if (rootTag.Equals("ENEMY_SITUATION"))
                {
                    Console.WriteLine("Received ENEMY_SITUATION");
                    ENEMY_SITUATION obj = new ENEMY_SITUATION();
                    obj.FromXml(xml);
                    Console.WriteLine("Object XML: " + obj.ToXml());
                }
                else if (rootTag.Equals("ForceTracker"))
                {
                    Console.WriteLine("Received ForceTracker");
                    ForceTracker obj = new ForceTracker();
                    obj.FromXml(xml);
                    Console.WriteLine("Object XML: " + obj.ToXml());
                }
                else if (rootTag.Equals("Georef"))
                {
                    Console.WriteLine("Received Georef");
                    Georef obj = new Georef();
                    obj.FromXml(xml);
                    Console.WriteLine("Object XML: " + obj.ToXml());
                }
                else if (rootTag.Equals("GRAPHICS"))
                {
                    Console.WriteLine("Received GRAPHICS");
                    GRAPHICS obj = new GRAPHICS();
                    obj.FromXml(xml);
                    Console.WriteLine("Object XML: " + obj.ToXml());
                }
                else if (rootTag.Equals("INDICATIONS_AND_WARNINGS"))
                {
                    Console.WriteLine("Received INDICATIONS_AND_WARNINGS");
                    INDICATIONS_AND_WARNINGS obj = new INDICATIONS_AND_WARNINGS();
                    obj.FromXml(xml);
                    Console.WriteLine("Object XML: " + obj.ToXml());
                }
                else if (rootTag.Equals("MTIReport"))
                {
                    Console.WriteLine("Received MTIReport");
                    MTIReport obj = new MTIReport();
                    obj.FromXml(xml);
                    Console.WriteLine("Object XML: " + obj.ToXml());
                }
                else if (rootTag.Equals("Observed_Position"))
                {
                    Console.WriteLine("Received Observed_Position");
                    Observed_Position obj = new Observed_Position();
                    obj.FromXml(xml);
                    Console.WriteLine("Object XML: " + obj.ToXml());
                }
                else if (rootTag.Equals("OrgStatus"))
                {
                    Console.WriteLine("Received OrgStatus");
                    OrgStatus obj = new OrgStatus();
                    obj.FromXml(xml);
                    Console.WriteLine("Object XML: " + obj.ToXml());
                }
                else if (rootTag.Equals("PositionReport"))
                {
                    Console.WriteLine("Received PositionReport");
                    PositionReport obj = new PositionReport();
                    obj.FromXml(xml);
                    Console.WriteLine("Object XML: " + obj.ToXml());

                    Console.WriteLine("");
                    Console.WriteLine("====================");
                    if (obj.PositionReport_Type_Choice.Unit != null)
                    {
                      Console.WriteLine("Unit name=" + obj.PositionReport_Type_Choice.Unit.Name);
                      Console.WriteLine("Unit URN=" + obj.PositionReport_Type_Choice.Unit.URN);
                    }
                    else if (obj.PositionReport_Type_Choice.Platform != null)
                    {
                      Console.WriteLine("Platform name=" + obj.PositionReport_Type_Choice.Platform.Name);
                      Console.WriteLine("Platform URN=" + obj.PositionReport_Type_Choice.Platform.URN);
                    }
                    Console.WriteLine("Latitude=" + obj.Location.Latitude);
                    Console.WriteLine("Longitude=" + obj.Location.Longitude);
                    Console.WriteLine("====================");
                    Console.WriteLine("");
                    
                }
                else if (rootTag.Equals("RAM-Events"))
                {
                    Console.WriteLine("Received RAM-EVENTS");
                    RAM_Events obj = new RAM_Events();
                    obj.FromXml(xml);
                    Console.WriteLine("Object XML: " + obj.ToXml());
                }
                else if (rootTag.Equals("RangeFan"))
                {
                    Console.WriteLine("Received RangeFan");
                    RangeFan obj = new RangeFan();
                    obj.FromXml(xml);
                    Console.WriteLine("Object XML: " + obj.ToXml());
                }
                else if (rootTag.Equals("SectorList"))
                {
                    Console.WriteLine("Received SectorList");
                    SectorList obj = new SectorList();
                    obj.FromXml(xml);
                    Console.WriteLine("Object XML: " + obj.ToXml());
                }
                else if (rootTag.Equals("SensorList"))
                {
                    Console.WriteLine("Received SensorList");
                    SensorList obj = new SensorList();
                    obj.FromXml(xml);
                    Console.WriteLine("Object XML: " + obj.ToXml());
                }
                else if (rootTag.Equals("SIGACT"))
                {
                    Console.WriteLine("Received SIGACT");
                    SIGACT obj = new SIGACT();
                    obj.FromXml(xml);
                    Console.WriteLine("Object XML: " + obj.ToXml());
                }
                else if (rootTag.Equals("Symbol"))
                {
                    Console.WriteLine("Symbol");
                    Symbol obj = new Symbol();
                    obj.FromXml(xml);
                    Console.WriteLine("Object XML: " + obj.ToXml());
                }
                else if (rootTag.Equals("Target"))
                {
                    Console.WriteLine("Received Target");
                    Target obj = new Target();
                    obj.FromXml(xml);
                    Console.WriteLine("Object XML: " + obj.ToXml());
                }
                else if (rootTag.Equals("Task_Org"))
                {
                    Console.WriteLine("Received Task_Org");
                    Task_Org obj = new Task_Org();
                    obj.FromXml(xml);
                    Console.WriteLine("Object XML: " + obj.ToXml());
                }
                else if (rootTag.Equals("TrackPointReport"))
                {
                    Console.WriteLine("Received TrackPointReport");
                    TrackPointReport obj = new TrackPointReport();
                    obj.FromXml(xml);
                    Console.WriteLine("Object XML: " + obj.ToXml());
                }
                else if (rootTag.Equals("TrackSet"))
                {
                    Console.WriteLine("Received TrackSet");
                    TrackSet obj = new TrackSet();
                    obj.FromXml(xml);
                    Console.WriteLine("Object XML: " + obj.ToXml());
                }
                else if (rootTag.Equals("UAVReport"))
                {
                    Console.WriteLine("Received UAVReport");
                    UAVReport obj = new UAVReport();
                    obj.FromXml(xml);
                    Console.WriteLine("Object XML: " + obj.ToXml());
                }
                else if (rootTag.Equals("WeaponList"))
                {
                    Console.WriteLine("Received WeaponList");
                    WeaponList obj = new WeaponList();
                    obj.FromXml(xml);
                    Console.WriteLine("Object XML: " + obj.ToXml());
                }
                else if (rootTag.Equals("WeatherWarnAndFcst2"))
                {
                    Console.WriteLine("Received WeatherWarnAndFcst2");
                    WeatherWarnAndFcst2 obj = new WeatherWarnAndFcst2();
                    obj.FromXml(xml);
                    Console.WriteLine("Object XML: " + obj.ToXml());
                }
                else
                {
                    Console.WriteLine("Received Unknown Payload: "+xml);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR: {0}", e.Message);
                Console.WriteLine(e.StackTrace);
            }
            Console.WriteLine();
        }
    }

    #endregion
  }
   
}
